/*
 * Copyright (c) ycq_work@163.com
 */

package com.mxg.blog.controller.articleAdmin;

import com.mxg.blog.common.CqLogger;
import com.mxg.blog.common.DetermineLogin;
import com.mxg.blog.common.ResultCode;
import com.mxg.blog.service.ArticleCategoryService;
import com.mxg.blog.service.ArticleService;
import com.mxg.blog.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/5/30 8:04
 * 删除文章分类
 */
@Controller
public class DeleteCategory {
    CqLogger cqLogger = CqLogger.getInstance();

    private final ArticleCategoryService articleCategoryService;
    private final ArticleService articleService;
    private final UserService userService;

    public DeleteCategory(ArticleCategoryService articleCategoryService,
                          ArticleService articleService,
                          UserService userService){
        this.articleCategoryService = articleCategoryService;
        this.articleService = articleService;
        this.userService = userService;
    }

    @PostMapping("api/deleteCategory")
    public ResponseEntity<Map<String, Object>> deleteCategory (@RequestBody Map<String, String> requestMap){
        Map<String, Object> resultMap = new HashMap<>();

        DetermineLogin determineLogin = DetermineLogin.getInstance(requestMap.get("userAccount"), userService);

        Integer userId;

        // 判断是否登录并获取userId
        if(!determineLogin.startJudge().equals(DetermineLogin.NOT_LOGIN)){
            userId = determineLogin.startJudge();
        }else {
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_NOT_LOGIN);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_NOT_LOGIN);

            return ResponseEntity.ok(resultMap);
        }

        Integer categoryId;

        try{
            categoryId = new Integer(requestMap.get("category"));
        }catch (Exception e){
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_GET_INFORMATION);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_GET_INFORMATION);
            cqLogger.print("出错" + e);
            return ResponseEntity.ok(resultMap);
        }

        // 按照分类查询, 查询该分类下是否还有文章
        if(articleService.accordingToClassificationTheQuery(userId, categoryId).isEmpty()){
            // 没有文章之后删除该分类
            articleCategoryService.deleteArticleCategoryByIds(categoryId);

            resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
            resultMap.put(ResultCode.message, ResultCode.SUCCESS_CODE);
        }else{
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_WRONG_EDIT_CATEGORY);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_WRONG_EDIT_CATEGORY);
        }

        return ResponseEntity.ok(resultMap);
    }
}
